package com.siyoumi.app.modules.app_ess.service;

import com.siyoumi.app.entity.EssClassUser;
import com.siyoumi.app.entity.EssUser;
import com.siyoumi.app.entity.SysUser;
import com.siyoumi.app.modules.app_ess.vo.EssCancelClass;
import com.siyoumi.app.modules.app_ess.vo.VaEssUser;
import com.siyoumi.app.modules.user.service.SvcSysUser;
import com.siyoumi.app.service.EssClassUserService;
import com.siyoumi.app.service.EssUserService;
import com.siyoumi.component.XApp;
import com.siyoumi.component.XBean;
import com.siyoumi.component.XSpringContext;
import com.siyoumi.component.http.InputData;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.exception.EnumSys;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.service.IWebService;
import com.siyoumi.util.XDate;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

//学生老师
@Slf4j
@Service
public class SvcEssUser
        implements IWebService {
    static public SvcEssUser getBean() {
        return XSpringContext.getBean(SvcEssUser.class);
    }

    static public EssUserService getApp() {
        return EssUserService.getBean();
    }


    /**
     * 老师列表
     */
    public List<EssUser> getTeacherList() {
        JoinWrapperPlus<EssUser> query = listQuery(1);

        return getApp().get(query);
    }

    public JoinWrapperPlus<EssUser> listQuery(Integer type) {
        JoinWrapperPlus<EssUser> query = listQuery(InputData.getIns());
        query.eq("euser_type", type);
        return query;
    }

    public JoinWrapperPlus<EssUser> listQuery() {
        return listQuery(InputData.getIns());
    }

    /**
     * select
     *
     * @return query
     */
    public JoinWrapperPlus<EssUser> listQuery(InputData inputData) {
        String name = inputData.input("name");
        String classId = inputData.input("class_id");
        String type = inputData.input("type");

        JoinWrapperPlus<EssUser> query = getApp().join();
        query.join(SysUser.table(), SysUser.tableKey(), EssUser.tableKey());
        query.eq("euser_x_id", XHttpContext.getX());

        if (XStr.hasAnyText(type)) { //0：学生；1：老师；
            query.eq("euser_type", type);
        } else {
            query.in("euser_type", 0, 1);
        }
        if (XStr.hasAnyText(name)) { //名称
            query.like("eclass_name", name);
        }
        if (XStr.hasAnyText(classId)) { //班级
            String sqlExists = "SELECT 1 FROM wx_app_x.t_ess_class_user WHERE ecu_class_id = {0} AND ecu_user_id = user_id";
            query.exists(sqlExists, classId);
        }

        return query;
    }

    public XReturn edit(InputData inputData, VaEssUser vo) {
        List<String> ignoreField = new ArrayList<>();
        if (inputData.isAdminEdit()) {
            //ignoreField.add("eclass_acc_id");
        }

        return XApp.getTransaction().execute(status -> {
            XReturn r = getApp().saveEntity(inputData, vo, false, ignoreField);
            SysUser entityUser = SvcSysUser.getApp().getEntity(vo.getEuser_id());
            SysUser entityUserUpdate = new SysUser();
            XBean.copyProperties(vo, entityUserUpdate);
            entityUserUpdate.setUser_id(vo.getEuser_id());
            entityUserUpdate.setUser_x_id(XHttpContext.getX());
            if (entityUser == null) {
                //新建，设置初始密码
                entityUserUpdate.setUser_pwd(XApp.encPwd(XHttpContext.getX(), "123456"));
            }

            SvcSysUser.getApp().saveOrUpdatePassEqualField(entityUser, entityUserUpdate);

            //清缓存
            getApp().delEntityCache(vo.getEuser_id());
            SvcSysUser.getApp().delEntityCache(vo.getEuser_id());
            return r;
        });
    }

    /**
     * 删除
     */
    @SneakyThrows
    @Transactional(propagation = Propagation.MANDATORY)
    public XReturn delete(List<String> ids) {
        XReturn r = XReturn.getR(0);

        getApp().delete(ids);

        return r;
    }


    /**
     * 取消分班
     *
     * @param vo
     */
    @Transactional(rollbackFor = Exception.class)
    public XReturn cancelClass(EssCancelClass vo) {
        List<EssUser> listUser = getApp().get(vo.getIds());
        for (EssUser entity : listUser) {
            if (Objects.equals(entity.getEuser_type(), 1)) {
                //老师，不能取消，到老师编辑页操作
                continue;
            }

            EssClassUser entityClassUser = SvcEssClass.getBean().getEntityClassUser(entity.getKey());
            if (entityClassUser != null) { //删除关系
                EssClassUserService.getBean().delete(entityClassUser.getKey());
            }
        }
        return EnumSys.OK.getR();
    }
}
